package com.spzx.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.dto.ProductInfoDto;
import com.spzx.product.dto.SkuQuery;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.mapper.ProductSpecMapper;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import com.spzx.product.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ProductSkuServiceImpl implements ProductSkuService {

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSpecMapper productSpecMapper;

    @Override
    public List<ProductSku> findProductSkuNo20() {
        return productSkuMapper.findProductSkuNo20();
    }

    @Override
    public List<ProductSku> skuList(SkuQuery query) {
        return productSkuMapper.skuList(query);
    }

    @Override
    public ProductInfoDto getProductInfoBySkuId(Long skuId) {
        ProductInfoDto productInfoDto = new ProductInfoDto();
        // 查询productSku的详情信息
        ProductSku productSku = productSkuMapper.getSkuInfoById(skuId);
        productInfoDto.setProductSku(productSku);
        // 查询product信息
        ProductVo productVo = productService.getInfo(productSku.getProductId());
        productInfoDto.setProductVo(productVo);
        // 查询specValue信息
        List<ProductSpec> specList = productSpecMapper.selectProductSpecListByCategoryId(productVo.getCategory3Id());
        if (CollUtil.isNotEmpty(specList)) {
            productInfoDto.setSpecValueList(specList.get(0).getSpecValue());
        }
        return productInfoDto;
    }

    @Override
    public ProductSku getSkuInfoBySkuId(Long skuId) {
        return productSkuMapper.getSkuInfoById(skuId);
    }

    @Override
    public Map<Long, BigDecimal> getSkuPrice(Set<String> skuIds) {
        List<ProductSku> list = productSkuMapper.getSkuPrice(skuIds);
        Map<Long, BigDecimal> collect = list.stream().collect(Collectors.toMap(ProductSku::getId, ProductSku::getSalePrice));
        return collect;
    }
}
